<template>
  <div class="home">
    <!-- 英雄区域 -->
    <section class="hero-section">
      <div class="hero-content">
        <h1 class="hero-title">校园活动报名系统</h1>
        <p class="hero-subtitle">发现精彩活动，参与校园生活，创造美好回忆</p>
        <div class="hero-buttons">
          <el-button type="primary" size="large" @click="goToActivities">
            <el-icon><Calendar /></el-icon>
            浏览活动
          </el-button>
          <el-button size="large" @click="goToLogin" v-if="!userStore.isLoggedIn">
            <el-icon><User /></el-icon>
            立即登录
          </el-button>
        </div>
      </div>
      <div class="hero-image">
        <el-image
          src="/images/campus-activity.svg"
          alt="校园活动"
          fit="contain"
        />
      </div>
    </section>

    <!-- 特色功能 -->
    <section class="features-section">
      <div class="container">
        <h2 class="section-title">系统特色</h2>
        <div class="features-grid">
          <div class="feature-card">
            <div class="feature-icon">
              <el-icon><Search /></el-icon>
            </div>
            <h3>活动查询</h3>
            <p>快速查找感兴趣的校园活动，支持多种筛选条件</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">
              <el-icon><EditPen /></el-icon>
            </div>
            <h3>在线报名</h3>
            <p>便捷的在线报名流程，实时查看报名状态</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">
              <el-icon><Management /></el-icon>
            </div>
            <h3>活动管理</h3>
            <p>组织者可以轻松发布和管理活动信息</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">
              <el-icon><ChatDotRound /></el-icon>
            </div>
            <h3>反馈评价</h3>
            <p>参与活动后可以提交反馈和评价</p>
          </div>
        </div>
      </div>
    </section>

    <!-- 热门活动 -->
    <section class="popular-activities" v-if="popularActivities.length > 0">
      <div class="container">
        <h2 class="section-title">热门活动</h2>
        <div class="activities-grid">
          <div 
            v-for="activity in popularActivities" 
            :key="activity.id"
            class="activity-card"
            @click="goToActivityDetail(activity.id)"
          >
            <div class="activity-header">
              <h3 class="activity-title">{{ activity.title }}</h3>
              <el-tag :type="getActivityStatus(activity).type" size="small">
                {{ getActivityStatus(activity).text }}
              </el-tag>
            </div>
            <div class="activity-meta">
              <div class="meta-item">
                <el-icon><Clock /></el-icon>
                <span>{{ formatDate(activity.startTime, 'MM-DD HH:mm') }}</span>
              </div>
              <div class="meta-item">
                <el-icon><Location /></el-icon>
                <span>{{ activity.location }}</span>
              </div>
              <div class="meta-item">
                <el-icon><User /></el-icon>
                <span>{{ activity.currentParticipants || 0 }}/{{ activity.maxParticipants || '不限' }}</span>
              </div>
            </div>
            <p class="activity-description">{{ activity.description }}</p>
          </div>
        </div>
        <div class="text-center mt-20">
          <el-button @click="goToActivities">查看更多活动</el-button>
        </div>
      </div>
    </section>

    <!-- 统计数据 -->
    <section class="stats-section">
      <div class="container">
        <div class="stats-grid">
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalActivities }}</div>
            <div class="stat-label">总活动数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalStudents }}</div>
            <div class="stat-label">注册学生</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalRegistrations }}</div>
            <div class="stat-label">总报名数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalOrganizers }}</div>
            <div class="stat-label">活动组织者</div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { formatDate, getActivityStatus } from '@/utils'
import api from '@/utils/api'
import {
  Calendar,
  User,
  Search,
  EditPen,
  Management,
  ChatDotRound,
  Clock,
  Location
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

const popularActivities = ref([])
const stats = ref({
  totalActivities: 0,
  totalStudents: 0,
  totalRegistrations: 0,
  totalOrganizers: 0
})

const goToActivities = () => {
  if (userStore.isLoggedIn) {
    router.push('/student/activities')
  } else {
    router.push('/login')
  }
}

const goToLogin = () => {
  router.push('/login')
}

const goToActivityDetail = (id) => {
  if (userStore.isLoggedIn) {
    router.push(`/student/activities/${id}`)
  } else {
    router.push('/login')
  }
}

const loadPopularActivities = async () => {
  try {
    const response = await api.get('/activities/popular')
    popularActivities.value = response.data.data || []
  } catch (error) {
    console.error('加载热门活动失败:', error)
  }
}

const loadStats = async () => {
  try {
    const response = await api.get('/activities/overview')
    stats.value = response.data.data || stats.value
  } catch (error) {
    console.error('加载统计数据失败:', error)
  }
}

onMounted(() => {
  loadPopularActivities()
  loadStats()
})
</script>

<style scoped>
.home {
  min-height: 100vh;
}

/* 英雄区域 */
.hero-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 80px 20px;
  display: flex;
  align-items: center;
  min-height: 500px;
}

.hero-content {
  flex: 1;
  max-width: 600px;
  margin: 0 auto;
}

.hero-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 20px;
  line-height: 1.2;
}

.hero-subtitle {
  font-size: 1.2rem;
  margin-bottom: 40px;
  opacity: 0.9;
  line-height: 1.6;
}

.hero-buttons {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.hero-image {
  flex: 1;
  max-width: 500px;
  margin: 0 auto;
}

/* 特色功能 */
.features-section {
  padding: 80px 20px;
  background: #fff;
}

.section-title {
  text-align: center;
  font-size: 2.5rem;
  font-weight: 600;
  margin-bottom: 60px;
  color: #303133;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 40px;
  max-width: 1000px;
  margin: 0 auto;
}

.feature-card {
  text-align: center;
  padding: 40px 20px;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
}

.feature-icon {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  color: white;
  font-size: 2rem;
}

.feature-card h3 {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 15px;
  color: #303133;
}

.feature-card p {
  color: #606266;
  line-height: 1.6;
}

/* 热门活动 */
.popular-activities {
  padding: 80px 20px;
  background: #f5f7fa;
}

.activities-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.activity-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #606266;
}

/* 统计数据 */
.stats-section {
  padding: 80px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 40px;
  max-width: 800px;
  margin: 0 auto;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 1.1rem;
  opacity: 0.9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .hero-section {
    flex-direction: column;
    text-align: center;
    padding: 60px 20px;
  }
  
  .hero-title {
    font-size: 2rem;
  }
  
  .hero-subtitle {
    font-size: 1rem;
  }
  
  .section-title {
    font-size: 2rem;
  }
  
  .features-section,
  .popular-activities,
  .stats-section {
    padding: 60px 20px;
  }
  
  .features-grid {
    grid-template-columns: 1fr;
    gap: 30px;
  }
  
  .activities-grid {
    grid-template-columns: 1fr;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 30px;
  }
  
  .stat-number {
    font-size: 2rem;
  }
}
</style>